<?php
/**
 * 
 * 根据话题抓取微博数据
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","cron_taobao_search_goods");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/taobao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/taobao/inc.php");

include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/qq/inc.php");

class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		global $config;
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$mblog_from_api = new mblog_from_api();
		$db_cron_get_manage = new db_cron_get_manage();
		foreach($config["site_name"] as $site_id => $site_name)
		{
			if(!$site_id)continue;
			$this->get_list_by_topic($site_name,$site_id);
			
			foreach($config["site_alias"] as $alias)
			{
				$this->get_list_by_topic($alias,$site_id,1);
			}
			
			
		}
		
		
		
		
	}	
	
	function get_list_by_topic($site_name,$site_id)
	{
		
		$db_taobao_goods_item = new db_taobao_goods_item();
		$c = new TopClient;
		$c->appkey = TAOBAO_APP_KEY;
		$c->secretKey = TAOBAO_APP_SECRET;
		$req = new TaobaokeItemsGetRequest;
		$req->setFields("num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume");
		$req->setKeyword($site_name);
		
		
	/*	默认排序:default 
		price_desc(价格从高到低) 
		credit_desc(信用等级从高到低) 
		commissionRate_desc(佣金比率从高到低)
		commissionNum_desc(成交量成高到低)
		commissionVolume_desc(总支出佣金从高到低) 
		delistTime_desc(商品下架时间从高到低) 
		*/
		
		$req->setStartCommissionRate("2000");
		$req->setEndCommissionRate("5000");
		
		//$req->setEndCommissionNum("1000000");
		$req->setSort("commissionVolume_desc");
		//$req->setCid(11);
		/*$req->setStartPrice("100");
		$req->setEndPrice("999");*/
		
		
		/*$req->setNick("jay");
		$req->setPid(123456);
		
		$req->setStartPrice("1");
		$req->setEndPrice("999");
		$req->setAutoSend("true");
		$req->setArea("杭州");
		$req->setStartCredit("1heart");
		$req->setEndCredit("1heart");
		$req->setGuarantee("true");
		
		$req->setStartCommissionNum("1000");
		$req->setEndCommissionNum("10000");
		$req->setStartTotalnum("1");
		$req->setEndTotalnum("10");
		$req->setCashCoupon("true");
		$req->setVipCard("true");
		$req->setOverseasItem("true");
		$req->setSevendaysReturn("true");
		$req->setRealDescribe("true");
		$req->setOnemonthRepair("true");
		$req->setCashOndelivery("true");
		$req->setMallItem("true");
		$req->setPageNo(1);
		$req->setPageSize(40);
		$req->setOuterCode("abc");
		$req->setIsMobile("true");*/
		$resp = $c->execute($req);
	
		echo "taobaodata_:";
		var_dump($get_data);
		$get_data = $resp["taobaoke_items"]["taobaoke_item"] ;
		$get_data = array_values($get_data);

		foreach($get_data as $value)
		{
			
			if($value["pic_url"])
			{
				/*$res = get_image::get_image_size_by_url($value["pic_url"]);
				$srcW = $res["width"]; 
				$srcH = $res["height"]; 
				$value["pic_width"] = $pic_width_pubu = TAOBAO_PUBU_WIDTH;
				$value["pic_height"] = $pic_height_pubu = intval($srcH/($srcW/$pic_width_pubu));
				*/
				$value["title"] = preg_replace("/\<.*?\>/","",$value["title"]);
				$db_taobao_goods_item->cron_insert_goods($value,$site_id);
				
			}
			
		}
		
		
			
	}
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
